package cn.tedu.bonfire.front.content.controller;

import cn.tedu.bonfire.common.pojo.vo.PageData;
import cn.tedu.bonfire.common.web.JsonResult;
import cn.tedu.bonfire.front.content.pojo.vo.CategoryListItemVO;
import cn.tedu.bonfire.front.content.pojo.vo.CategoryStandardVO;
import cn.tedu.bonfire.front.content.pojo.vo.CategoryTreeItemVO;
import cn.tedu.bonfire.front.content.service.ICategoryService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Validated
@RequestMapping("/categories")
@Api(tags="2. 分类管理")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/tree")
    @ApiOperation("查询类别树")
    @ApiOperationSupport(order = 420)
    public JsonResult listTree() {
        List<CategoryTreeItemVO> categoryTree = categoryService.listTree();
        return JsonResult.ok(categoryTree);
    }

    @ApiOperation("根据ID查询类别详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    @GetMapping("/{id:[0-9]+}")
    public JsonResult getStandardById(@Range(min = 1, message = "查询类别详情失败，请提交有效的ID值！")
                                      @PathVariable Long id) {
        CategoryStandardVO queryResult = categoryService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @ApiOperation("根据父级查询子级类别列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父级类别ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int")
    })
    @GetMapping("/list-by-parent")
    public JsonResult getStandardById(@Range(message = "查询子级类别列表失败，请提交有效的父级类别ID值！") Long parentId,
                                      @Range(min = 1, message = "查询子级类别列表失败，请提交有效的页码！") Integer page) {
        Integer pageNum = page == null ? 1 : page;
        PageData<CategoryListItemVO> pageData = categoryService.listByParent(parentId, pageNum, 5);
        return JsonResult.ok(pageData);
    }
}
